package com.kaoshi.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.entity.Xiangmu;
import com.kaoshi.service.IXiangmuService;
import com.kaoshi.service.impl.XiangmuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-25
 */
@RestController
@CrossOrigin
@RequestMapping("/xiangmu")
public class XiangmuController {
    @Autowired
    private IXiangmuService xiangmuService;

    @RequestMapping("/getAll")
    public Result getAll(Integer current,Integer pageSize,@RequestBody Xiangmu xiangmu){
        Page<Xiangmu> xiangmuPage = new Page<>(current, pageSize);
        LambdaQueryWrapper<Xiangmu> qw = new LambdaQueryWrapper<>();
        if (xiangmu.getName()!=null && xiangmu.getName()!=""){
            qw.eq(Xiangmu::getName,xiangmu.getName());
        }
        if (xiangmu.getXmlx()!=null){
            qw.eq(Xiangmu::getXmlx,xiangmu.getXmlx());
        }
        qw.eq(Xiangmu::getDel,1);
        Page<Xiangmu> page = xiangmuService.page(xiangmuPage, qw);
        return Result.ok(page);
    }
    @RequestMapping("update")
    public Result update(@RequestBody Xiangmu xiangmu){
        return Result.ok(xiangmuService.updateById(xiangmu));
    }

//    @RequestMapping("del")
//    public Result add(@RequestBody Xiangmu xiangmu){
//        xiangmu.setDel(0);
//        return Result.ok(xiangmuService.updateById(xiangmu));
//    }
    @RequestMapping("save")
    public Result save(@RequestBody Xiangmu xiangmu){
        if (xiangmu.getId()!=null){
            return Result.ok(xiangmuService.updateById(xiangmu));
        }else {
            return Result.ok(xiangmuService.save(xiangmu));
        }
    }
    @RequestMapping("queryById")
    public Result queryById(Integer id){
        return Result.ok(xiangmuService.list(new LambdaQueryWrapper<Xiangmu>().eq(Xiangmu::getId,id)).get(0));
    }
    @RequestMapping("del")
    public Result del(Integer id){
        List<Xiangmu> list = xiangmuService.list(new LambdaQueryWrapper<Xiangmu>().eq(Xiangmu::getId, id));
        Xiangmu xiangmu = list.get(0);
        xiangmu.setDel(0);
        xiangmuService.updateById(xiangmu);
        return Result.ok();
    }
}
